package com.marion.mzuul.config;

import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ExpiryPolicyBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CacheConfig {

    /**
     * 设置CacheManager
     */
    @Bean("AppEhCacheManager")
    public CacheManager getCacheManager() {
        CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build();
        cacheManager.init();
        return cacheManager;
    }

    /**
     * 设置默认
     */
    @Bean("appEhCache")
    public Cache appEhCache() {
        return getCacheManager().createCache("appEhCache",
                CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class,
                        String.class,
                        ResourcePoolsBuilder.heap(1000000)
                )
                        .withExpiry(ExpiryPolicyBuilder.noExpiration())
                        .build()
        );
    }
}
